package com.med.service.impl;

import com.med.dto.RespondDTO;
import com.med.entity.CommentsDO;
import com.med.entity.UserDO;
import com.med.mapper.MineMapper;
import com.med.service.IMineService;
import com.med.util.IdentificationOCRUtil;
import com.med.vo.CommentsVO;
import com.med.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: internet-based-healthcare
 * @BelongsPackage: com.med.service.impl
 * @Author: 小吴
 * @CreateTime: 2025-08-26 14:46
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class MineServiceImpl implements IMineService {

    @Autowired
    private MineMapper mineMapper;

    @Autowired
    private IdentificationOCRUtil identificationOCRUtil;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * @Description: 根据id查询个人信息
     * @Param:
     * @return:
     */
    @Override
    public RespondDTO queryUser(UserVO userVO) {
        UserDO queryUser = mineMapper.queryUser(userVO);
        if (queryUser != null) {
            return RespondDTO.success(queryUser);
        }
        return null;
    }

    /**
    * @Description: 个人中心修改
    * @Param:
    * @return:
    */
    @Override
    public RespondDTO updateUser(UserVO userVO) {
        System.err.println("service" + userVO);
        //从redis获取yzm
        String phoneCode = redisTemplate.opsForValue().get("mineCode");
        //用户输入的验证码
        String inputCode = userVO.getCode();
        if (phoneCode == null) {
            return new RespondDTO(-1, "验证码过期，请重新获取", null);
        }
        if (!inputCode.equals(phoneCode)) {
            return new RespondDTO(-2, "验证码错误，请重新输入", null);
        }

        String frontPath = null;
        //获取前部分的路径身份证
        String frontIdCard = userVO.getFrontIdCard();

        // 替换路径
        if (frontIdCard != null && frontIdCard.startsWith("http://127.0.0.1:9300/statics/")) {
            String newPath = frontIdCard.replace("http://127.0.0.1:9300/statics/", "/");
            //System.err.println("替换后路径: " + newPath);
            frontPath = "D:/ruoyi/uploadPath" + newPath;
        }

        HashMap<String, String> identificationCard = identificationOCRUtil.getIdentificationCard(frontPath);
        String idNumber = identificationCard.get("idNumber");
        String userIdCard = userVO.getUserIdCard();
        if (!userIdCard.equalsIgnoreCase(idNumber)) {
            return new RespondDTO(-3, "请输入正确的身份证号码", null);
        }


        Integer updateUser = mineMapper.updateUser(userVO);
        if (updateUser != null) {
            return RespondDTO.success(updateUser);
        }
        return null;
    }

    /**
    * @Description: 我的评论--根据用户id来查询对医生的评论
    * @Param: 
    * @return:  
    */
    @Override
    public RespondDTO queryComments(CommentsVO commentsVO) {
        List<CommentsDO> queryComments = mineMapper.queryComments(commentsVO);
        if (queryComments != null) {
            return RespondDTO.success(queryComments);
        }
        return null;
    }

    /**
    * @Description: 删除评论
    * @Param:
    * @return:
    */
    @Override
    public RespondDTO updateComments(CommentsVO commentsVO) {
        Integer updateComments = mineMapper.updateComments(commentsVO);
        if (updateComments != null) {
            return RespondDTO.success(updateComments);
        }
        return null;
    }
}
